<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-9-4
 * Time: 上午10:44
 * To change this template use File | Settings | File Templates.
 */
class yyets_com extends MY_Model
{
    private $data = array();

    public function analysis(array $arr)
    {
        $content = $this->getFileContent($arr['file_name']);
        $download = array();

        if (empty ($content)) return false;

        //来源网站ID
        $this->data['source_id'] = $arr['source_id'];

        //分类，中文名，英文名，海报地址
        preg_match("/var share = {.*title:'【(.*)】《(.*)》\((.*)\).*pic:'(.*)'/sU", $content, $class);
        $this->data['class'] = empty ($class[1]) ? '' : $this->getClass($class[1]);
        $this->data['china_name'] = empty ($class[2]) ? '' : $class[2];
        $this->data['english_name'] = empty ($class[3]) ? '' : $class[3];
        $this->data['img_addr'] = empty ($class[4]) ? '' : $class[4];

        //导演
        preg_match("/<span>导演：<\/span>(.*)<\/li>/sU", $content, $director);
        $this->data['director'] = isset ($director[1]) ? $director[1] : '';

        //主演
        preg_match("/<span>主演：<\/span>(.*)<\/li>/sU", $content, $starring);//主演
        $this->data['starring'] = isset ($starring[1]) ? str_replace('/', '|', $starring[1]) : '';//implode('/', explode('/', $starring[1]));

        //地区
        preg_match("/<span>地区：<\/span>.*<strong>(.*)<\/strong>/sU", $content, $country);
        $this->data['area'] = isset ($country[1]) ? $country[1] : '';//empty ($country[1]) ? '' : $country[1];

        //类型
        preg_match("/类.*型：<\/font>(.*)<\/li>/sU", $content, $type);
        $this->data['type'] = isset ($type[1]) ? str_replace('/', '|', $type[1]) : '';//implode('|', array_filter($type[1]));

        //上映日期
        preg_match("/[上映日期|首播日期]：<\/font>.*<label id=\"pubdate\">(.*)<\/label>/sU", $content, $releases);
        $this->data['public_time'] = isset ($releases[1]) ? $releases[1] : '';//empty ($releases[1]) ? '' : $releases[1];

        //版本
        $this->data['version'] = '多版本';

        //对白语言
        preg_match("/<span>语言：<\/span>.*<strong>(.*)<\/strong>/sU", $content, $dialog);
        $this->data['language'] = isset ($dialog[1]) ? $dialog[1] : '';//empty ($dialog[1]) ? '' : $dialog[1];

        //简介
        preg_match("/<p.*id=\"description\".*>.*<div.*>(.*)<\/div>/sU", $content, $summary);
        $this->data['drama'] = isset ($summary[1]) ? $summary[1] : '';//empty ($summary[1]) ? '' : $summary[1];

        //集数
        $this->data['episode'] = '';

        //配音
        $this->data['dub'] = '';

        //下载链接<script type="text/javascript" src="/html/resource/
        preg_match("/<script.*type=\"text\/javascript\".*src=\"(\/html\/resource\/.*\/items.html)/sU", $content, $downloadLink);

        $download = $downloadLink[1];

        //echo 'http://www.yyets.com'.$download."\n";return;

        usleep(1000);
        $downloadList = file_get_contents('http://www.yyets.com'.$download);

        preg_match("/var items = (.*);/sU", $downloadList, $downloadStr);
        $downloads = json_decode($downloadStr[1], true);

        //echo '<pre>';print_r($downloads);exit;
        if (empty ($downloads)) $downloads = array();
        $download = array();
        foreach ($downloads as $v) {
            foreach ($v as $v2) {
                $download[] = array('name'=> '['.$v2['format'].']'.$v2['name'], 'downaddr'=>$v2['way']['thunder_url'], 'size' => $v2['size']);
            }
        }

        $this->data['download'] = $download;
        //echo '<pre>';print_r($download);exit;
        //echo '<pre>';print_r($this->data);

        //echo $content;
    }

    /**
     * 保存数据
     *
     * @return mixed
     */
    public function save()
    {
        //echo '<pre>';print_r($this->data);exit;
        $data = array(
            'china_name' => $this->data['china_name'],
            'english_name' => $this->data['english_name'],
            'class' => $this->data['class'],
            'director' => $this->data['director'],
            'starring' => $this->data['starring'],
            'area' => $this->data['area'],
            'type' => $this->data['type'],
            'update_status' => 1,
            'public_time' => $this->data['public_time'],
            'version' => $this->data['version'],
            'language' => $this->data['language'],
            'poster_addr' => '',
            'drama' => addslashes($this->data['drama']),
            'episode' => $this->data['episode'],
            'dub' => $this->data['dub'],
            'status' => 1,
            'uniqid' => md5('yyets_com'.$this->data['source_id']),
            'img_addr' => $this->data['img_addr'],
            'source' => 'yyets_com',
            'source_id' => $this->data['source_id'],
            'score' => '',
            'subtitle' => '',
            'release' => '',
            'update_time' => date('Y-m-d H:i:s'),
            'create_time' => date('Y-m-d H:i:s'),
        );

        $this->db->insert('movie', $data, true);
        $mId = $this->db->insert_id();
        if ($mId) {
            $this->categoryCorrespond($mId, $this->data['class'], $this->data['type']);
        }
        $this->saveDownLoad($mId);

        return $mId;
    }

    /**
     * 保存下载链接地址
     *
     * @param $mId
     */
    public function saveDownLoad($mId)
    {
        if (empty ($this->data['download']) || !is_array($this->data['download'])) {
            $this->data['download'] = array();
        }
        if (!$mId || empty ($mId)) {
            $this->load->model('movie');

            $data = $this->movie->getMovieByChinaName($this->data['china_name']);
            if (!empty ($data)) {
                $mId = $data['id'];
            }
        }

        foreach ($this->data['download'] as $v) {
            if (empty ($v) || !is_array($v)) continue;

            $data = array(
                'video_id' => $mId,
                'name' => $v['name'],
                'download_addr' => $v['downaddr'],//
                'size' => $v['size'],
                'uniqueid' => md5($mId.$v['name'].$v['downaddr'].$v['size']),
                'source_download_link' => $v['downaddr'],
                'create_time' => date('Y-m-d H:i:s'),
            );
            //echo '<pre>';print_r($data);exit;
            $this->db->insert('download_list', $data, true);

            $downloadId = $this->db->insert_id();
            if ($downloadId) {
                $this->updateResource($mId);
            }
        }
        unset ($this->data['download']);
    }
}
